//
//  VCTicketInfomation.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/2/10.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VCTicketInfomation: VCBase {
    
    private lazy var ticketInfoView:TicketInfoView = {
        let ui = TicketInfoView()
        ui.delegate = self
        ui.frame = CGRect(x: 0, y: realHeight + MARGIN10, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - realHeight - MARGIN10)
        return ui
    }()
    
    public var orderInfo:OrderInfo?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        init_view()
        init_data()
    }
}

extension VCTicketInfomation:TicketInfoViewDelegate {
    
    func ticket_delete(orderid: String) {
        // 拼接数据
        let request = NSMutableDictionary()
        request.setValue(orderid, forKey: "orderid")
        // 请求
        self.doPost(api: "/delete/order", dict: request) { (dict) in
            self.show(text: "删除成功")
            self.navigationController?.popViewController(animated: true)
        }
    }
    
    func ticket_finish(orderid: String) {
        // 拼接数据
        let request = NSMutableDictionary()
        request.setValue(orderid, forKey: "orderid")
        // 请求
        self.doPost(api: "/modify/order", dict: request) { (dict) in
            self.show(text: "修改成功")
            self.navigationController?.popViewController(animated: true)
        }
    }
    
    private func init_data() -> Void {
        // 拼数据
        let request = NSMutableDictionary()
        request.setValue(orderInfo?.userid, forKey: SP_USERID)
        request.setValue(orderInfo?.linkmanid, forKey: SP_LINKMANID)
        request.setValue(orderInfo?.passengerid, forKey: SP_PASSENGERID)
        // 请求
        self.doPost(api: "search/order/account", dict: request) { (dict) in
            if self.isServerSuccess(dict: dict) != 1200 {
                self.show(text: "未搜到相应信息")
                return
            }
            // 解析数据
            let bodyRequest = dict.value(forKey: SP_BODYREQUEST) as! NSDictionary
            let account = OrderAccount(dict: bodyRequest as! [String : String])
            // 更新数据
            self.ticketInfoView.account = account
            self.ticketInfoView.orderInfo = self.orderInfo
        }
    }
    
    private func init_view() -> Void {
        view.addSubview(backroundWhite)
        view.addSubview(ticketInfoView)
    }
}
